<template>
	<el-dialog
	  title="选择考察方案"
	  :visible.sync="visible"
	  :before-close="close"
    :append-to-body="true"
	  :close-on-click-modal="false"
    width="80%">
    <el-transfer
      v-model="selection"
      :data="projects"
      :titles="['考察方案', '已选择']">
    </el-transfer>
	  <span slot="footer" class="dialog-footer">
	    <el-button @click="close">取 消</el-button>
	    <el-button type="primary" class="pan-btn primary-btn" @click="showGet">下一步</el-button>
  	</span>

    <get
      v-if="getVisible"
      @close="closeInner('getVisible')"
      @submit="close"
      :childDataForm="grandDataForm" />

	</el-dialog>
</template>

<script>
  import { getStabilityProjectByProductId,getProjectCount } from '@/api/stability'
  import { getZcList } from '@/api/check'
	import { CODE_OK } from '@/common/js/config'
  import { isBlank } from '@/common/js/utils'
  import Get from './get'
	export default {
		data() {
			return {
				visible: false,
        onlyPlan: false,
        getVisible: false,
        list: {},
        zcList: {},
        projects: [],
        selection: [],
        grandDataForm: ''
			}
		},
		props: ["childDataForm"],
		created() {
			if (!isBlank(this.childDataForm)) {
        this.onlyPlan = this.childDataForm.onlyPlan == undefined ? false : this.childDataForm.onlyPlan
        this.list = this.childDataForm
        this.getInfo()
			}
		},
    components: {
    	Get
    },
		methods: {
      getInfo() {
        const params = {
          productId: this.list.productId,
          page: 1,
          limit: 999
        }
        const params1 = {
          checkId: this.list.checkId
        }
        getProjectCount(params).then(res => {
          if (res.code === CODE_OK) {
            if (res.count == 0) {
              if (this.onlyPlan) {
                this.$message.error("此货品还未绑定方案，请先制定方案并绑定此货品！！")
                this.$emit('close')
                return
              }
              //只领取
              this.$confirm('此货品没有绑定方案，确定领取?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(async () => {
                getZcList(params1).then(res => {
                  if (res.code === CODE_OK) {
                    this.zcList = res.zcList
                    let temPlan = {
                      planName: this.list.productName+'('+this.list.batchNum+')'+'【已领取未计划】',
                      checkId: this.list.checkId,
                      workshopId: this.zcList.workshopId,
                      workshopName: this.zcList.workshopName,
                      productionDate: this.zcList.productionDate,
                      validityDate: this.zcList.validityDate,
                      productId: this.list.productId,
                      productGmp: this.list.productGmp,
                      productName: this.list.productName,
                      productSpec: this.list.productSpec,
                      productPackSpec: this.list.productPackSpec,
                      batchNum: this.list.batchNum,
                      inventory: this.list.groupGetQuantity,
                      inventoryUnitId: this.list.groupGetUnitId,
                      inventoryUnitName: this.list.groupGetUnitName,
                      incomingMini: this.list.groupGetQuantity,
                      incomingMiniUnitId: this.list.groupGetUnitId,
                      incomingMiniUnitName: this.list.groupGetUnitName,
                      incomingBig: this.list.groupGetQuantity,
                      incomingBigUnitId: this.list.groupGetUnitId,
                      incomingBigUnitName: this.list.groupGetUnitName,
                      storeDate: null,
                      status: 1,
                      remark: this.list.remark
                    }
                    this.grandDataForm = {
                      onlyGet: true,
                      list: this.list,
                      projects: [temPlan],
                      zcList: this.zcList
                    }
                    this.visible = true
                    this.$nextTick(() => {
                      this.showGet()
                    })
                  }else{
                    this.$message.error(res.msg)
                    this.$emit('close')
                  }
                })
              }).catch(() => {
                this.$emit('close')
              })
            }else{
              //领取和制定计划
              getStabilityProjectByProductId(params).then(res => {
                if (res.code === CODE_OK) {
                  let ps = res.info.projects
                  for (let i=0,j=ps.length; i<j; i++) {
                    this.$set(ps[i],'key',i)
                    this.$set(ps[i],'label','【' + ps[i].stabilityModeName + '】' + ps[i].projectName)
                    this.projects.push(ps[i])
                  }
                  getZcList(params1).then(res => {
                    if (res.code === CODE_OK) {
                      this.zcList = res.zcList
                      this.visible = true
                      this.grandDataForm = {
                        onlyPlan: this.onlyPlan,
                        list: this.list,
                        projects: [],
                        zcList: this.zcList
                      }
                    }else{
                      this.$message.error(res.msg)
                      this.$emit('close')
                    }
                  })
                }else{
                  this.$message.error(res.msg)
                  this.$emit('close')
                }
              })
            }
          }else{
            this.$message.error(res.msg)
          }
        })
      },
			close(val) {
				this.$emit('close')
				if (val == 'submit') {
				  this.$emit('refreshData')
				}
				this.visible = false
			},
      closeInner(val) {
        this[val] = false
      },
			showGet() {
        if (this.projects.length != 0 && this.selection.length == 0) {
          this.$message.error('请选择方案进行制定入库计划')
          return
        }
        if (this.grandDataForm.projects.length == 0) {
          this.grandDataForm.projects = this.selection.map(v => {
            return this.projects[v]
          })
        }
        this.getVisible = true
			}
    }
	}
</script>

<style scoped>
  >>> .el-transfer-panel{
    width: 40%;
  }
</style>
